﻿GY.Bill.Consult = {
    //检测是否输入完成
    CheckInput: function () {
        var billAmount = $("#txtAmount").val();
        var billMethod = $("#div_BillMethod").find(".active").attr("data-val");
        var bankId = $("#txtBank").attr("data-val");
        var beginDate = $("#txtBeginDate").val();
        var endDate = $("#txtEndDate").val();
        var billState = $("#div_BillState").find(".active").attr("data-val");
        var openBillTime = $("#txtOpenBillTime").val();
        var billLocation = $("#txtBillLocation").val();

        var msg = "";

        if (bankId == "") {
            msg = "请选择付款行";
            return msg;
        }

        if (billAmount == "" || (billAmount / 1 <= 0)) {
            msg = "请输入正确的金额";
            return msg;
        }

        if (beginDate == "") {
            msg = "请选择出票日";
            return msg;
        }

        if (endDate == "") {
            msg = "请选择到期日";
            return msg;
        }

        if (billState == undefined) {
            msg = "请选择是否已开票";
            return msg;
        }

        if (billMethod == undefined) {
            msg = "请选择操作方式";
            return msg;
        }

        if (billState == "2" && openBillTime == "") {
            msg = "请选择票何时开好";
            return msg;
        }

        if (billLocation == "") {
            msg = "请填写票在哪儿";
            return msg;
        }
        return msg;
    },
    GetBillModel: function () {
        var billType = $("#divMain").attr("data-type");
        var billAmount = $("#txtAmount").val();
        var billMethod = $("#div_BillMethod").find(".active").attr("data-val");
        var bankId = $("#txtBank").attr("data-val");
        var beginDate = $("#txtBeginDate").val();
        var endDate = $("#txtEndDate").val();
        var billState = $("#div_BillState").find(".active").attr("data-val");
        var openBillTime = $("#txtOpenBillTime").val();
        var billLocation = $("#txtBillLocation").val();
        var model = {
            BillType: billType,
            BillAmount: billAmount,
            BillMethod: billMethod,
            BankId: bankId,
            BeginDate: beginDate,
            EndDate: endDate,
            BillState: billState,
            OpenBillTime: openBillTime,
            BillLocation: billLocation,
            Remark: $("#txtRemark").val()
        }
        return model;
    },
    //保存票据
    SaveBill: function (obj) {
        $.ajax({
            type: "Post",
            url: $("#divMain").attr("data-url"),
            data: GY.Bill.Consult.GetBillModel(),
            dataType: "json",
            success: function (result) {
                if (result.code == 1) {
                    //GY.Bill.Common.LoadInvite(result.data);
                    window.location.href = $("#divMain").attr("data-done-url").replace("ConsultId", result.data).replace("ConsultId", result.data);
                }
            }
        });
    }
};
(function () {
    $(function () {
        //输入票据 金额
        $("#txtAmount").focus(function () {
            fnShade($(".shade"));
            $(".shade").show();
            $(this).closest("dl").addClass("z15");
        });
        $("#txtAmount").blur(function () {
            $(".shade").hide();
            $(this).closest("dl").removeClass("z15");
        });

        $("#div_BillState").on("tap", "label", function () {
            $(this).parent().find("i").removeClass("active");
            $(this).children("i").addClass("active");

            if ($(this).find("i").attr("data-val") == "2") {
                $("#dl_OpenBillTime").show();
            } else {
                $("#dl_OpenBillTime").hide();
            }
        });

        $("#div_BillMethod").on("tap", "label", function () {
            $(this).parent().find("i").removeClass("active");
            $(this).children("i").addClass("active");
        });

        //立即发布
        $("#btnQuote").on("tap", function () {
            var msg = GY.Bill.Consult.CheckInput();
            if (msg != "") {
                //显示错误消息
                ShowTip(msg);
                return;
            } else {
                fnShade($(".shade"));
                $(".shade").show();
                $(".mask_tip").show();
            }
        });

        //确认发布票据
        $("#btnSure").on("tap", function () {
            var flag = GY.Bill.Common.SaveCheck($(this));

            if (flag) {
                GY.Bill.Consult.SaveBill();
            } 
        });

        //修改用户信息并保存报价信息
        $("#btnSaveUserOffer").on("tap", function () {
            var flag = GY.Bill.Common.SaveUser();

            if (flag) {
                $("#btnSure").attr("data-check", 1);
                GY.Bill.Consult.SaveBill();
            }
        });
    });
})();

